﻿using PMS.Core.Infrastructure;
using PMS.Data.Entities.Settings;

namespace PMS.Web.Framework.Models
{
    /// <summary>
    /// DavidLee 改为与Layui前端参数对应
    /// </summary>
    public abstract partial class BaseSearchModel : BaseEntityModel, IPagingRequestModel
    {
        #region Fields
        private int _pageIndex;
        #endregion

        #region Ctor

        public BaseSearchModel()
        {
        }

        #endregion

        #region Properties
        /// <summary>
        /// Gets a page number 对应分页列表的页码数
        /// </summary>
        public int Page
        {
            get => _pageIndex;
            set 
            {
                if (value <= 0) _pageIndex = 1;
                _pageIndex = value; 
            }
        }
        /// <summary>
        /// Gets a page size 对应Layui table limit: 每页显示的条数（默认：10）。值务必对应 limits 参数的选项。
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// Gets or sets a comma-separated list of available page sizes
        /// 对应Layui table limits 每页条数的选择项，默认：[10,20,30,40,50,60,70,80,90]。 
        /// </summary>
        public string AvailablePageSizes { get; set; }

        /// <summary>
        /// Gets or sets skiping number of rows count. Paging first record indicator.
        /// </summary>
        public int Start
        {
            get { return (_pageIndex > 1) ? ((_pageIndex - 1) * PageSize) + 1 : 0; }
        }
        #endregion

        #region Methods

        /// <summary>
        /// Set grid page parameters
        /// </summary>
        public void SetGridPageSize()
        {
            var adminAreaSettings = EngineContext.Current.Resolve<AdminAreaSettings>();
            SetGridPageSize(adminAreaSettings.DefaultGridPageSize, adminAreaSettings.GridPageSizes);
        }

        /// <summary>
        /// Set popup grid page parameters
        /// </summary>
        public void SetPopupGridPageSize()
        {
            var adminAreaSettings = EngineContext.Current.Resolve<AdminAreaSettings>();
            SetGridPageSize(adminAreaSettings.PopupGridPageSize, adminAreaSettings.GridPageSizes);
        }

        /// <summary>
        /// Set grid page parameters
        /// </summary>
        /// <param name="pageSize">Page size; pass null to use default value</param>
        /// <param name="availablePageSizes">Available page sizes; pass null to use default value</param>
        public void SetGridPageSize(int pageSize, string availablePageSizes = null)
        {
            Page = 1;
            PageSize = pageSize;
            AvailablePageSizes = availablePageSizes;
        }

        #endregion
    }
}
